package cn.appsys.controller.backenduser;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.pojo.BackendUser;
import cn.appsys.service.backenduser.BackendUserService;
import cn.appsys.tools.Constants;
/**
 * 后台用户登录控制器
 * @author 寇超强
 *
 */
@Controller
public class LoginController {
	@Resource
	BackendUserService backendUserService;
	
	/**
	 * 进入登陆界面
	 */
	@RequestMapping("/userLogin.html")
	public String backendUserLogin(){
		return "backend/userLogin";
	}
	/**
	 * 验证登陆
	 * @return
	 */
	@RequestMapping(value="/deLogin", method= RequestMethod.POST)
	public String deLogin(@RequestParam("codeName") String codeName,
						@RequestParam("userPassword") String userPassword,
						HttpServletRequest request,
						HttpSession session){
		BackendUser user=backendUserService.backenlogin(codeName, userPassword);
		if(user==null){
			//用户不存在
			request.setAttribute("error", "用户不存在");
			return "backend/userLogin";
		}else{
			//登陆成功
			if(user.getUserPassword().equals(userPassword)){
				session.setAttribute(Constants.USER_SESSION, user);
				return "backend/main";		//登陆成功
			}else{
				//密码错误
				request.setAttribute("error", "密码错误");
				return "backend/userLogin";
			}
		}
	}
}
